package thread.Test;

//Demo1.java的源码
class MyThreadE extends Thread {
 
 public MyThreadE(String name) {
     super(name);
 }

 @Override
 public void run() {
     try {  
         int i=0;
         while (!isInterrupted()) {
             Thread.sleep(100); // 休眠100ms
             i++;
             System.out.println(Thread.currentThread().getName()+" ("+this.getState()+") loop " + i);  
         }
     } catch (InterruptedException e) {  
         System.out.println(Thread.currentThread().getName() +" ("+this.getState()+") catch InterruptedException.");  
     }
 }
};

public class interuptTest {

 public static void main(String[] args) {  
     try {  
         MyThreadE t1 = new MyThreadE("t1");  // 新建“线程t1”
         System.out.println(t1.getName() +" ("+t1.getState()+") is new.");  

         t1.start();                      // 启动“线程t1”
         System.out.println(t1.getName() +" ("+t1.getState()+") is started.");  

         // 主线程休眠300ms，然后主线程给t1发“中断”指令。
         Thread.sleep(300);
         t1.interrupt();
         System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted.");

         // 主线程休眠300ms，然后查看t1的状态。
         Thread.sleep(300);
         System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted now.");
     } catch (InterruptedException e) {  
         e.printStackTrace();
     }
 } 
}
/*
t1 (NEW) is new.
t1 (RUNNABLE) is started.
t1 (RUNNABLE) loop 1
t1 (RUNNABLE) loop 2
t1 (RUNNABLE) catch InterruptedException.
t1 (TERMINATED) is interrupted.
t1 (TERMINATED) is interrupted now.

*/